//
//  LocationSelectViewController.m
//  LubanWork
//
//  Created by guduzhonglao on 2/14/20.
//  Copyright © 2020 xiaohao. All rights reserved.
//

#import "LocationSelectViewController.h"
#import "ProvinceSelectCell.h"

@interface LocationSelectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView*table;
@property(nonatomic,strong)NSArray*data;
@end

@implementation LocationSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"定位";
    self.table=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.table.bounces=NO;
    self.table.dataSource=self;
    self.table.delegate=self;
    self.table.rowHeight=kSCRATIO(40);
    self.table.backgroundColor=kColorFromRGBHex(0xFCF9FE);
    self.table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.right.left.bottom.equalTo(self.view);
    }];
    self.data=[NSArray arrayWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"ProvinceList" withExtension:@"plist"]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kSCRATIO(33);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    if (section==1) {
        return 4;
    }
    return [self.data count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* h =[[UIView alloc] init];
    h.backgroundColor=kColorFromRGBHex(0xFCF9FE);
    UILabel*title=[UILabel CreatLabeltext:@"" Font:kFONT(14) Textcolor:kColorFromRGBHex(0x775FFB) textAlignment:NSTextAlignmentLeft];
    if (section==0) {
        title.text=@"当前定位省份";
    }else if (section==1){
        title.text=@"热门省份";
    }else if (section==2){
        title.text=@"全部省份";
    }
    [h addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kSCRATIO(16.5));
        make.centerY.equalTo(h);
    }];
    return h;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProvinceSelectCell*cell=[tableView dequeueReusableCellWithIdentifier:@"SelectProvince"];
    if (!cell) {
        cell=[[ProvinceSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SelectProvince"];
    }
    if (indexPath.section==0) {
        if (![UtilTools isBlankString:[AppDelegate shareAppDelegate].province]) {
            cell.name.text=[AppDelegate shareAppDelegate].province;
        }else{
            cell.name.text=@"地球";
        }
    }else{
        cell.name.text=self.data[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        [AppDelegate shareAppDelegate].province=[AppDelegate shareAppDelegate].location;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProvice" object:nil];
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [AppDelegate shareAppDelegate].province=self.data[indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateProvice" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
